DEFINE VARIABLE vcHost     AS CHARACTER    INITIAL "www.saferpay.com"  NO-UNDO.
DEFINE VARIABLE vcPort     AS CHARACTER    INITIAL "443"       NO-UNDO.
DEFINE VARIABLE vcparam    AS CHARACTER                         NO-UNDO.

DEFINE VARIABLE vhSocket   AS HANDLE                            NO-UNDO.
CREATE SOCKET vhSocket.

vhSocket:CONNECT('-H ' + vcHost + ' -S ' + vcPort) NO-ERROR.
IF vhSocket:CONNECTED() = FALSE THEN
DO:
    MESSAGE "Connection failure" VIEW-AS ALERT-BOX.
    MESSAGE ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX.
    RETURN.
END.
ELSE
   MESSAGE "Connect" VIEW-AS ALERT-BOX.
   vhSocket:SET-READ-RESPONSE-PROCEDURE('getResponse').

/* supposes there is an webspeed app called yourapp.w that receives

param1, param2, param3 */

/* Vcparam = "?spPassword=JFrY6io@qi"                                                                  */
/*         + "&ACCOUNTID=99867-94913159"                                                               */
/*         + "&ORDERID=WEB-12345"                                                                      */
/*         + "&AMOUNT=5000"                                                                            */
/*         + "&CURRENCY=CHF"                                                                           */
/*         + "&LANGID=fr"                                                                              */
/*         + "&DESCRIPTION=Test"                                                                       */
/*         + "&SUCCESSLINK=http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/pmt_ok.htm?xxx="  */
/*         + "&FAILLINK=http://www.parvis.ch/cgi-bin/cgiip.exe/WService=wsbroker1/pmt_nok.htm?xxx=".   */

RUN PostRequest (INPUT '/hosting/CreatePayInit.asp',vcparam).

WAIT-FOR READ-RESPONSE OF vhSocket.

vhSocket:DISCONNECT() NO-ERROR.

DELETE OBJECT vhSocket.
QUIT.

PROCEDURE getResponse:
    DEFINE VARIABLE vcWebResp    AS CHARACTER        NO-UNDO.
    DEFINE VARIABLE lSucess      AS LOGICAL          NO-UNDO.
    DEFINE VARIABLE mResponse    AS MEMPTR           NO-UNDO.

    IF vhSocket:CONNECTED() = FALSE THEN do:
        MESSAGE 'Not Connected' VIEW-AS ALERT-BOX.
        RETURN.
    END.

    lSucess = TRUE.

    DO WHILE vhSocket:GET-BYTES-AVAILABLE() > 0:
       SET-SIZE(mResponse) = vhSocket:GET-BYTES-AVAILABLE() + 1.
       SET-BYTE-ORDER(mResponse) = BIG-ENDIAN.
       vhSocket:READ(mResponse,1,1,vhSocket:GET-BYTES-AVAILABLE()).
       vcWebResp = vcWebResp + GET-STRING(mResponse,1).
    END.
END.

PROCEDURE PostRequest:
    DEFINE VARIABLE vcRequest      AS CHARACTER.
    DEFINE VARIABLE mRequest       AS MEMPTR.
    DEFINE INPUT PARAMETER postUrl AS CHAR. 
    DEFINE INPUT PARAMETER postData AS CHAR.
    vcRequest = 'POST ' + postUrl + ' HTTP/1.0rn' +
            'Content-Type: text/xmlrn' +  
            'Content-Length:' + string(LENGTH(postData)) + 'rn' +
             'rn' +
             postData + 'rn'.
    MESSAGE vcREquest VIEW-AS ALERT-BOX.
    SET-SIZE(mRequest)            = 0.
    SET-SIZE(mRequest)            = LENGTH(vcRequest) + 1.
    SET-BYTE-ORDER(mRequest)      = BIG-ENDIAN.
    PUT-STRING(mRequest,1)        = vcRequest .
    vhSocket:WRITE(mRequest, 1, LENGTH(vcRequest)).
END PROCEDURE.
